package com.henry.framework.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created by 峥桂 on 2017/5/7.
 */
public final class JsonUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class);

    private static final ObjectMapper OBJECT_MAPPER=new ObjectMapper();


    public static <T> String toJson(T obj) {
        String json;
        try {
            json= OBJECT_MAPPER.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            LOGGER.error("convert POJO to JSON failure",e);
            throw  new RuntimeException(e);
        }
        return json;
    }

    /**
     * json 转为pojo
     * @param json
     * @param type
     * @param <T>
     * @return
     */
    public static <T> T fromJson(String json,Class<T> type) {
        T pojo;
        try {
            pojo= OBJECT_MAPPER.readValue(json,type);
        }  catch (IOException e) {
            LOGGER.error("convert JSON  to POJO failure",e);
            throw  new RuntimeException(e);
        }
        return pojo;
    }
}
